package com.lengxf.leetcode.a;

import java.util.Arrays;

public class Leetcode_quickSort {

    static int[] arr = GenerateArr.getArr(10);

    static int count = 0;

    public static void main(String[] args) {
        System.out.println("开始 : " + Arrays.toString(arr));
        quickSort(arr);
        System.out.println("结束 : " + Arrays.toString(arr));
    }

    private static void quickSort(int[] arr) {
        if (arr == null || arr.length == 0) {
            return;
        }
        reFn(arr, 0, arr.length - 1);
    }

    /**
     * 递归体
     * By Lengxf 2023-10-08 10:24:31
     **/
    private static void reFn(int[] arr, int left, int right) {
        if (left >= right) {
            return;
        }
        //左右形成分界
        int index = left + 1;
        for (int i = index; i <= right; i++) {
            if (arr[i] < arr[left]) {
                swap(arr, i, index);
                index++;
            }
        }
        //分界点数值交换
        swap(arr, left, index - 1);
        //左右分别递归处理
        reFn(arr, left, index - 1);
        reFn(arr, index, right);
    }

    private static void swap(int[] arr, int i1, int divide) {
        int tmp = arr[i1];
        arr[i1] = arr[divide];
        arr[divide] = tmp;
    }


}
